package com.study;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author sjw
 * @className Test
 * @date 2020/10/21 16:09
 * @description: TODO
 */
public class Test {
    public static void main(String[] args) throws ParseException {

        Calendar lastCalendar = Calendar.getInstance();
        lastCalendar.set(Calendar.MONTH,0);
        System.out.println(lastCalendar.getTime());
        lastCalendar.add(Calendar.MONTH, -1);
        lastCalendar.set(Calendar.HOUR_OF_DAY, 0);
        lastCalendar.set(Calendar.MINUTE, 0);
        System.out.println(lastCalendar.getTime());

//        Calendar lastCalendar = Calendar.getInstance();
//        lastCalendar.
//
//        System.out.println(new Date().getDate());
//        System.out.println(new Date().getDay());
//
//        Calendar calendar1 = Calendar.getInstance();
//        calendar1.add(Calendar.MONTH, -1);
//        calendar1.set(Calendar.DAY_OF_MONTH,1);
//        // 时
//        calendar1.set(Calendar.HOUR_OF_DAY, 0);
//        // 分
//        calendar1.set(Calendar.MINUTE, 0);
//        // 秒
//        calendar1.set(Calendar.SECOND, 0);
//        System.out.println(calendar1.getTime());
//
//
//
//        //Date d  =Date.from()
////        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
////        String s = "2011-07-09";
////        Date date = formatter.parse(s);
////        System.out.println(date);
//
//        Calendar calendar2 = Calendar.getInstance();
//        calendar2.set(Calendar.YEAR, 2017);
//        System.out.println(calendar2.getTime());
//
//
//
//
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.MONTH, -1);// 获取上个月
//        calendar.set(Calendar.DAY_OF_MONTH,1);// 获取上个月的第一天
//        System.out.println(calendar.getTime());
//        System.out.println(new Date().getDate());
//        System.out.println(new Date().getDay());
//        System.out.println(buildLuaScript());
//
//        List<User> users= Arrays.asList(new User(1,"2020-10-05"),new User(2,"2020-10-06"),new User(3,"2020-10-04"));
//
//        users=users.stream().sorted(Comparator.comparing(User::getDate)).collect(Collectors.toList());
//
//        users.forEach(user -> {
//            System.out.println(user.getId()+"  "+user.getDate());
//        });
//




    }


    public static  class User{
        private int id;
        private String date;

        public User(int id, String date){
            this.id=id;
            this.date=date;
        }

        public int getId() {
            return id;
        }

        public String getDate() {
            return date;
        }
    }


    public static String buildLuaScript() {
        StringBuilder lua = new StringBuilder();
        lua.append("local c");
        lua.append("\nc = redis.call('get',KEYS[1])");
        // 调用不超过最大值，则直接返回
        lua.append("\nif c and tonumber(c) > tonumber(ARGV[1]) then");
        lua.append("\nreturn c;");
        lua.append("\nend");
        // 执行计算器自加
        lua.append("\nc = redis.call('incr',KEYS[1])");
        lua.append("\nif tonumber(c) == 1 then");
        // 从第一次调用开始限流，设置对应键值的过期
        lua.append("\nredis.call('expire',KEYS[1],ARGV[2])");
        lua.append("\nend");
        lua.append("\nreturn c;");
        return lua.toString();
    }
}
